package net.qiyuesuo.dao.config;

import java.sql.Connection;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zaxxer.hikari.HikariDataSource;

/**
 * Mysql 数据库 连接管理实现
 * 使用 HikariCP 获取连接池
 */
public class ConnectionManager  {

	private static Logger logger = LoggerFactory.getLogger(ConnectionManager.class);
	//获取数据库连接池
	private static HikariDataSource da = DataSourceConfig.instance().getDataSource();
	
	/**
	 * 获取数据库连接
	 * @return
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			conn = da.getConnection();
			conn.setAutoCommit(false);
		} catch (SQLException e) {
			logger.error("获取数据库连接失败",e);
		}
		return conn;
	}
	
	/**
	 * 关闭数据库连接
	 * @param conn
	 */
	public static void closeConnection(Connection conn) {
		try {
			conn.close();
		} catch (SQLException e) {
			logger.error("关闭数据库连接失败",e);
		}
	}
	
}
